package com.clkj.xiuzheng.service.shangcheng;

import com.clkj.xiuzheng.dao.shangcheng.*;
import com.clkj.xiuzheng.pojo.shangcheng.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @author yawang
 * @create 2024/05/14 11:43:41
 **/
@Service
public class MainServiceImpl implements MainService{

    @Autowired
    private ScUserMapper userMapper;
    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private ScProductCategoryMapper categoryMapper;

    @Autowired
    private ScProductMapper productMapper;

    @Autowired
    private BannerMapper bannerMapper;

    @Autowired
    private ProductCartMapper productCartMapper;
    @Autowired
    private ArchiveTagMapper archiveTagMapper;
    @Autowired
    private ArchiveMapper archiveMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    private Map map;
    @Override
    public Map listMain(Integer userId, Integer companyId) {
        SUser user =userMapper.selectByPrimaryKey(userId);

        Notice notice =noticeMapper.getNotice(user.getUserType(),companyId);

        List<ProductCategory> categories =categoryMapper.listCategory();

        List<SProduct> productList = productMapper.listEightPro();

        //轮播图
        List<Banner> bannerList = bannerMapper.listBanner();

        map = new HashMap();
        map.put("user",user);
        map.put("notice", notice);
        map.put("categories", categories);
        map.put("productList", productList);
        map.put("bannerList",bannerList);
        map.put("isUpdate", redisTemplate.opsForValue().get("isUpdate_"+userId));

        return map;
    }

    @Override
    public void addCart(ProductCart cart) {
        productCartMapper.addOrUpdate(cart);
    }

    @Override
    public List<ProductCart> listCart(Integer userId) {

        return productCartMapper.listCart(userId);
    }

    @Override
    public List<SProduct> listDiamond() {
        Example example = new Example(SProduct.class);
        Example.Criteria criteria =  example.createCriteria();
        criteria.andEqualTo("productKind",1).andEqualTo("productStatus",1);
        example.orderBy("productSaleType").asc();
        return productMapper.selectByExample(example);
    }

    @Override
    public List<Notice> listNotice(Integer userType, Integer companyId) {
        return noticeMapper.listNotice(userType,companyId);
    }

    @Override
    public List<ArchiveTag> listHealth() {
        Example example = new Example(ArchiveTag.class);
        Example.Criteria criteria =  example.createCriteria();
        criteria.andEqualTo("parentId",0);
        List<ArchiveTag> archiveTags = archiveTagMapper.selectByExample(example);
        archiveTags.forEach(archiveTag -> {
            Example example1 = new Example(ArchiveTag.class);
            Example.Criteria criteria1 =  example1.createCriteria();
            criteria1.andEqualTo("parentId",archiveTag.getArchivesTagId());
            List<ArchiveTag> archiveTags1 = archiveTagMapper.selectByExample(example1);
            archiveTag.setList(archiveTags1);
        });
        return archiveTags;
    }

    @Override
    public void addHealth(Integer userId, String tagIds) {
        Archives archive = new Archives();
        archive.setUserId(userId);
        archive.setTagId(tagIds);
        archiveMapper.insert(archive);
    }

    @Override
    public List<ArchiveTag> getHealth(Integer userId) {
      Example example = new Example(Archives.class);
      Example.Criteria criteria =  example.createCriteria();
      criteria.andEqualTo("userId",userId);
      Archives archives = archiveMapper.selectOneByExample(example);
      String[] ids = archives.getTagId().split(",");
      List<ArchiveTag> archiveTags = new ArrayList<>();
      for (String id : ids) {
          Example example1 = new Example(ArchiveTag.class);
          Example.Criteria criteria1 =  example1.createCriteria();
          criteria1.andEqualTo("archivesTagId",Integer.parseInt(id));
          ArchiveTag archiveTag = archiveTagMapper.selectOneByExample(example1);
          archiveTags.add(archiveTag);
      }
      return archiveTags;
    }

    @Override
    public List<SUser> operatorGetUser(Integer userId){
        SUser user = userMapper.selectByPrimaryKey(userId);
        Example example = new Example(SUser.class);
        Example.Criteria criteria =  example.createCriteria();
        criteria.andEqualTo("parentId",user.getUserId());
        return userMapper.selectByExample(example);
    }

    @Override
    public void deleteCart(String cartIds) {
        List<Integer> idList = new ArrayList<>();
        String [] id = cartIds.split(",");
        for (String s : id) {
            idList.add(Integer.parseInt(s));
        }
        Example example = new Example(ProductCart.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("cartId",idList);
        productCartMapper.deleteByExample(example);
    }
}
